<template>
	<div id="friend-merchant-manage" class="w-h-100 pos-r flex-1 apply-container" style="box-sizing: border-box; overflow: hidden">
		<div class="w-h-100 ver-flex pos-a" style="box-sizing: border-box; padding: 6px">
			<div class="search-s page-public-s" style="box-sizing: border-box">
				<el-card shadow="never" :body-style="{ paddingBottom: '0' }">
					<el-form class="box-f" ref="queryForm" :inline="true">
						<!-- <el-form-item  label="岗位：">
							<el-select v-model="transferVal1" class="m-2" placeholder="请选择" style="width: 100%">
								<el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
						</el-form-item> -->
						<el-form-item>
							<el-button v-if="$hasPer('RiskManagement1:add')" @click="addNodeClick" icon="ele-Plus" type="primary"> 新增 </el-button>
						</el-form-item>
					</el-form>
				</el-card>
			</div>

			<div style="box-sizing: border-box" class="flex-1 pos-r">
				<div class="pos-a w-h-100" style="box-sizing: border-box">
					<el-card class="box-card w-h-100" shadow="never" style="margin-top: 16px">
						<div style="" class="w-h-100 ver-flex">
							<div class="flex-1 pos-r">
								<div class="pos-a w-h-100">
									<el-table :data="list" class="w-h-100" style="width: 100%" border v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.5)">
										<el-table-column type="index" label="序号" width="55" :show-overflow-tooltip="true" align="center" :index="indexMethod" />
										<el-table-column min-width="120" :show-overflow-tooltip="true" label="岗位名称" align="center">
											<el-table-column min-width="120" :show-overflow-tooltip="true" align="center">
												<template #header>
													<SearchModel typeId="name" typeVal="select" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
												</template>
												<template #default="{ row }">
													{{ row.name }}
												</template>
											</el-table-column>
										</el-table-column>
										<el-table-column min-width="200"  label="安全职责明细" align="center">
											<el-table-column min-width="200"  align="center">
												<template #header>
													<SearchModel typeId="detail" typeVal="input" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
												</template>
												<template #default="{ row }">
													<!-- {{ row.detail }} -->
													<el-input disabled  type="textarea" maxlength="2000" show-word-limit autosize v-model="row.detail" clearable />
												</template>
											</el-table-column>
										</el-table-column>
										<el-table-column min-width="120" :show-overflow-tooltip="true" prop="createdTime" label="创建时间" align="center" />
										<el-table-column min-width="120" :show-overflow-tooltip="true" prop="createdUserName" label="创建人" align="center" />
										<el-table-column min-width="120" :show-overflow-tooltip="true" label="更新时间" align="center">
											<template #default="scope">
												<span v-if="scope.row.updatedTime">{{ scope.row.updatedTime }}</span>
												<span v-else> - </span>
											</template>
										</el-table-column>
										<el-table-column min-width="120" :show-overflow-tooltip="true" label="更新人" align="center">
											<template #default="scope">
												<span v-if="scope.row.updatedUserName">{{ scope.row.updatedUserName }}</span>
												<span v-else> - </span>
											</template>
										</el-table-column>
										<el-table-column min-width="140" label="操作" align="center" fixed="right">
											<el-table-column min-width="140" align="center" fixed="right">
												<template #header>
													<el-button @click="searchClick" icon="ele-Search" type="primary" text> 查询 </el-button>
													<el-button @click="resetClick" icon="ele-Refresh" class="clear-bg" text> 重置 </el-button>
												</template>
												<template #default="scope">
													<div class="operate-s">
														<el-button @click="detailNodeClick(scope.row)" class="icon-e" icon="ele-View" size="small" text type="success"> 详情 </el-button>
														<el-button v-if="$hasPer('RiskManagement2:edit')" @click="nodeClick(scope.row)" class="icon-e" icon="ele-Edit" size="small" text type="primary"> 编辑 </el-button>
														<el-button v-if="$hasPer('RiskManagement3:delete')" @click="deleteHandle(scope.row.n_Id)" class="icon-e" icon="ele-Delete" size="small" text type="danger">
															删除
														</el-button>
													</div>
												</template>
											</el-table-column>
										</el-table-column>
									</el-table>
								</div>
							</div>
							<div style="height: 30px; margin-top: 20px" class="hr-flex-start">
								<el-pagination
									v-model:currentPage="pageNo"
									v-model:page-size="pageSize"
									:total="totalRows"
									:page-sizes="[10, 20, 50, 100]"
									small
									background
									@size-change="handleSizeChange"
									@current-change="handleCurrentChange"
									layout="total, sizes, prev, pager, next, jumper"
									class="w-h-100"
								/>
							</div>
						</div>
					</el-card>
				</div>
			</div>
		</div>
		<!-- 新增and编辑 -->
		<el-dialog draggable :close-on-click-modal="false" :close-on-press-escape="false" v-model="showDialogVisible" :title="saveId ? '编辑' : '新增'" width="40%" @close="cancelUseClick">
			<el-form label-width="140px" label-position="left" ref="ruleFormRef" :model="ruleForm" size="default" :rules="rules">
				<el-divider content-position="left"> 基本信息 </el-divider>
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item prop="jobId" label="岗位">
							<el-select v-model="ruleForm.jobId" class="m-2" placeholder="请选择" style="width: 100%">
								<el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item prop="detail" label="安全职责明细">
							<el-input type="textarea" maxlength="2000" show-word-limit autosize v-model="ruleForm.detail" placeholder="安全职责明细" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancelUseClick">取消</el-button>
					<el-button type="primary" @click="controlUseClick(ruleFormRef)">确定</el-button>
				</span>
			</template>
		</el-dialog>

		<!-- 查看详情 -->
		<el-dialog draggable :close-on-click-modal="false" :close-on-press-escape="false" v-model="showDetailDialogVisible" title="详情" width="50%" @close="cancelDetailClick">
			<el-divider content-position="left"> 基本信息 </el-divider>
			<el-descriptions class="margin-top" :column="2" border>
				<el-descriptions-item :span="2" >
					<template #label> 岗位名称 </template>
					{{ tableData.name }}
				</el-descriptions-item>
				<el-descriptions-item :span="2">
					<template #label> 安全职责明细 </template>
					{{ tableData.detail }}
				</el-descriptions-item>
				<el-descriptions-item width="180px">
					<template #label> 创建时间 </template>
					{{ tableData.createdTime }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label> 创建人 </template>
					{{ tableData.createdUserName }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label> 更新时间 </template>
					{{ tableData.updatedTime }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label> 更新人 </template>
					{{ tableData.updatedUserName }}
				</el-descriptions-item>
			</el-descriptions>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import getTableData from '/@/utils/getTableData';
import SearchModel from '/@/views/sms/component/search-model.vue';
import { SecurityResponsibilityCardQuery, SecurityResponsibilityCardUpdate, getPostDetailApiV2, SecurityResponsibilityCardCreate } from '/@/api/sms/HazardousChemicalsInterface';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { SecurityResponsibilityCardDelete } from '/@/api/sms/HazardousChemicalsInterface';

/***** 查询 *****/
let searchObj: any = reactive({});
const transferVal1 = ref();
const searchClick = () => {
	// searchObj.JobId = transferVal1.value;
	let newObj = Object.keys(searInfo.value);
	if (newObj.length !== 0) {
		newObj.map((i: any) => {
			searchObj[i] = searInfo.value[i];
		});
	}
};
const resetClick = () => {
	searInfo.value = {};
	let newObj = Object.keys(searchObj);
	if (newObj.length !== 0) {
		newObj.map((i: any) => {
			searchObj[i] = '';
			// console.log(i);
		});
	}
	initInfoFun();
};

/***** 新增 *****/
const showDialogVisible = ref(false);
const ruleForm: any = reactive({
	jobId: '',
	detail: '',
});
const addNodeClick = async () => {
	showDialogVisible.value = true;
};

/***** 编辑 *****/
const saveId = ref();
const nodeClick = async (row: any) => {
	if (row.n_Id) {
		saveId.value = row.n_Id;

		ruleForm.jobId = row.id;
		ruleForm.detail = row.detail;
	}
	showDialogVisible.value = true;
};

/*** 删除 */
const deleteHandle = async (val: String | Number) => {
	ElMessageBox.confirm('请确定删除此条数据', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let param = { id: val };
			try {
				let res: any = await SecurityResponsibilityCardDelete(param);
				if (res.success) {
					getData();
				}
			} catch (err) {
				return;
			}
		})
		.catch(() => {
			ElMessage({
				type: 'info',
				message: '删除失败',
			});
		});
};

/***** 新增表单校验和事件 *****/
const ruleFormRef = ref();
const rules = reactive({
	jobId: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
	detail: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
});
const cancelUseClick = () => {
	showDialogVisible.value = false;
	saveId.value = '';
	ruleForm.detail = '';
	ruleForm.jobId = '';
};
const controlUseClick = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			console.log('submit!');
			if (saveId.value) {
				SecurityResponsibilityCardUpdate({ ...ruleForm, id: saveId.value }).then(() => {
					cancelUseClick();
					getData();
				});
			} else {
				SecurityResponsibilityCardCreate(ruleForm).then(() => {
					cancelUseClick();
					getData();
				});
			}
		} else {
			console.log('error submit!', fields);
		}
	});
};

/*** detailHandle 查看详情事件 */
const showDetailDialogVisible = ref(false);
const tableData = ref();
const detailNodeClick = async (item: any) => {
	tableData.value = item;
	showDetailDialogVisible.value = true;
};
const cancelDetailClick = () => {
	showDialogVisible.value = false;
	tableData.value = {};
};

const { handleCurrentChange, pageSize, pageNo, handleSizeChange, totalRows, list, indexMethod, loading, getData } = getTableData(SecurityResponsibilityCardQuery, {}, searchObj);

/*** 初始化数据 */
const basicsInfo: any = ref([]);
const initInfoFun = () => {
	basicsInfo.value = [
		{
			typeId: 'name', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
			typeNam: '', // 输入类型(number,text等) --- 选填
			searchVal: 'JobId', // 查询字段 --- 必填
			defaultVal: '', // 字段默认值 --- 选填
			placeholder: '岗位名称', // placeholder数据
			defaultProp: options1.value, // 下拉框中使用，除下拉框外其它情况为空 --- 选填
			eventType: '', // 事件类型（如burl/change等） --- 选填
			defaultEvent: '', // 事件名称 --- 选填
		},
		{
			typeId: 'detail', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
			typeNam: '', // 输入类型(number,text等) --- 选填
			searchVal: 'Detail', // 查询字段 --- 必填
			defaultVal: '', // 字段默认值 --- 选填
			placeholder: '安全职责明细', // placeholder数据
			defaultProp: '', // 下拉框中使用，除下拉框外其它情况为空 --- 选填
			eventType: '', // 事件类型（如burl/change等） --- 选填
			defaultEvent: '', // 事件名称 --- 选填
		},
	];
};

const searInfo: any = ref();
const searchHandel = (val: any) => {
	searInfo.value = val;
};

const options1: any = ref([]);
onMounted(async () => {
	const res = await getPostDetailApiV2();
	res.data.forEach((element: any) => {
		options1.value.push({
			label: element.name,
			value: element.id,
		});
	});

	await initInfoFun();
});
</script>

<style lang="scss">
@import '../style/public.scss';
</style>

<style scoped lang="scss">
#friend-merchant-manage {
	:deep(.el-upload--picture-card) {
		--el-upload-picture-card-size: 90px;
		background-color: #cccccc56;
		margin-bottom: 2px;
	}

	:deep(.el-upload-list__item) {
		width: 90px;
		height: 90px;
	}

	.box-f {
		:deep(.el-form-item__label) {
			padding: 0;
		}
	}

	:deep(.el-card__body) {
		height: 100%;
	}

	:deep(.el-card) {
		border: none !important;
	}

	.upload-demo {
		margin-left: 12px;
		margin-top: 2px;
	}

	ul {
		list-style: none;

		li {
			cursor: pointer;

			&:hover {
				color: red;
			}
		}
	}

	.icon-e {
		:deep(.el-icon) {
			margin-right: 0px !important;
		}
	}

	.cardBox {
		margin: 6px;
		width: 100%;
		height: 100%;
		border: 1px solid #ccc;

		.cardOne {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #ccc;
			width: 100%;
			height: 40px;

			li {
				display: flex;
				align-items: center;

				&:nth-child(1) {
					display: flex;
					width: 50%;

					span {
						padding: 0 4px;
						width: 70px;
					}

					p {
						flex: 1;
					}
				}

				&:nth-child(2) {
					display: flex;
					width: 50%;

					span {
						padding: 0 4px;
						width: 70px;
					}

					p {
						flex: 1;
					}
				}
			}
		}

		.cardTwo {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #ccc;
			width: 100%;
			height: 200px;

			li {
				display: flex;
				align-items: center;

				&:nth-child(1) {
					display: flex;
					width: 50%;

					span {
						padding: 0 4px;
						width: 70px;
					}

					p {
						flex: 1;
					}
				}

				&:nth-child(2) {
					display: flex;
					width: 50%;

					span {
						padding: 0 4px;
						width: 70px;
					}

					p {
						flex: 1;
					}
				}
			}
		}

		.cardThree {
			display: flex;
			justify-content: center;
			align-items: center;
			border-bottom: 1px solid #ccc;
			width: 100%;
			height: 100px;

			p {
				&:nth-child(1) {
					padding: 0 4px;
					width: 70px;
				}

				&:nth-child(2) {
					flex: 1;
					text-align: center;

					.upload-s {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.cardFour {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			height: 40px;

			p {
				&:nth-child(1) {
					padding: 0 4px;
					width: 70px;
				}

				&:nth-child(2) {
					flex: 1;
					text-align: center;
				}
			}
		}
	}
}
</style>
